package com.spogher.bsc.pages;

import java.io.Serializable;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.spogher.bsc.data.dao.interfaces.ChampionshipDao;
import com.spogher.bsc.data.dao.interfaces.TeamDao;
import com.spogher.bsc.data.dataobjects.Championship;
import com.spogher.bsc.data.dataobjects.Team;
import com.spogher.components.dropdownChoices.ChampionshipDropDownChoice;
import com.spogher.components.dropdownChoices.TeamDropDownChoice;

public class ManageTeamPage extends BscTemplatePage{
	
	private static final String SUCCESS_MESSAGE = "team successfully updated";
	private static final String ERROR_MESSAGE = "an error occured, please try again";
	
	@SpringBean
	private TeamDao teamDao;
	@SpringBean
	private ChampionshipDao championshipDao;
	private String formFeedBackLabelMessage = "";

	public ManageTeamPage() {
		final Label feedbackLabel = new Label("formFeedback", new Model<String>(formFeedBackLabelMessage));
		feedbackLabel.setOutputMarkupId(true);
		add(feedbackLabel);
		Form<ManageTeamModelObject> manageTeamForm = new Form<ManageTeamModelObject>("manageTeamForm", 
				new CompoundPropertyModel<ManageTeamModelObject>(new ManageTeamModelObject()));
		
		DropDownChoice<Team> teamChoice = new TeamDropDownChoice("team", teamDao.findAll());
		manageTeamForm.add(teamChoice);
		
		DropDownChoice<Championship> championshipChoice = new ChampionshipDropDownChoice("championship", championshipDao.findAll());
		manageTeamForm.add(championshipChoice);
		
		
		manageTeamForm.add(new AjaxSubmitLink("submit"){
			private static final long serialVersionUID = 1L;

			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				ManageTeamModelObject manageTeamModelObject = (ManageTeamModelObject) form.getModelObject();
				manageTeamModelObject.getTeam().setChampionship(manageTeamModelObject.getChampionship());
				teamDao.save(manageTeamModelObject.getTeam());
				formFeedBackLabelMessage = SUCCESS_MESSAGE;
				feedbackLabel.setDefaultModel(new Model<String>(formFeedBackLabelMessage));
				target.addComponent(feedbackLabel);
			}
		});	
		add(manageTeamForm);

	}
	
	private class ManageTeamModelObject implements Serializable{

		private static final long serialVersionUID = 1L;
		private Championship championship;
		private Team team;
		public void setChampionship(Championship championship) {
			this.championship = championship;
		}
		public Championship getChampionship() {
			return championship;
		}
		public void setTeam(Team team) {
			this.team = team;
		}
		public Team getTeam() {
			return team;
		}
		
	}
}
